package webjoch.process.strategies;

import webjoch.Main;
import webjoch.misc.Functions;
import webjoch.process.strategies.puzzles.Traps;
import webjoch.wrappers.Door;
/**
 * 
 * @author Webjoch
 *
 */
public class SolvedPuzzle{
	static Traps traps = new Traps();
	
	/**
	 * Gets solved puzzles that needed to solve more times in the dungeon
	 * @return solved puzzles
	 */
	public static Puzzle getPuzzle(){
		for (int i = 0; i < Main.getPuzzles().length; i++){
			if (!Functions.inArray(Main.unSolvedPuzzles, i)) continue;
			Main.getPuzzles()[i].setRoom(Main.getDungeon().getCurrentRoom());
			if (Main.getPuzzles()[i].isValid()) return Main.getPuzzles()[i];
		}
		return null;
	}
	
	/**
	 * Solve the solved puzzles for more times
	 * @param door the destination 
	 * @return returns true if there is a puzzle that needed to solve for the second time
	 */
	public static boolean solve(Door door){
		Puzzle puzzle = getPuzzle();
		if (puzzle == null) return true;
		
		if (puzzle.getType() == Puzzle.Type.TRAPS){
			Main.ignoreCurrentRoom = true;
			traps.setRoom(Main.getDungeon().getCurrentRoom());
			if (traps.getBlockedDoorRotation() == door.getRotation()){
				traps.traverse();
				traps.loop();
			}
			if (traps.isFinished()){
				Main.ignoreCurrentRoom = false;
				return true;
			}
		}
		return true;
	}
}
